#include "UnitTest.h"


struct TestClass
{
    std::string str;
    u32 num;
};


DCL_UNITTEST( TestMemoryManager )
{
    const u32 testTimes = 100000;

    Timer timer( true );
    std::vector<TestClass> testSTL;
    for ( u32 i = 0; i < testTimes; ++i )
    {
        TestClass* pTestClass = new TestClass();
        testSTL.push_back( TestClass() );

        delete pTestClass;
        testSTL.pop_back();        
    }

    printf( "new and delete %u times: %ums\n", testTimes, timer.GetTime() );
}


static u32 __stdcall ThreadProc( void *arg )
{
    const u32 testTimes = 256;

    Timer timer( true, (u32)arg );
    std::vector<TestClass> testSTL;
    for ( u32 i = 0; i < testTimes; ++i )
    {
        TestClass* pTestClass = new TestClass();
        testSTL.push_back( TestClass() );

        delete pTestClass;
        testSTL.pop_back();        
    }

    printf( "Thread %d - new and delete %u times: %ums\n", timer.GetId(), testTimes, timer.GetTime() );

    return 0;
}

DCL_UNITTEST( TestMemoryManager_ThreadSafe )
{
    const u32 cThreadNum = 10;
    
    Thread threads[cThreadNum];

    for ( u32 i = 0; i < cThreadNum; ++i )
    {
        char name[256];
        sprintf_s( name, 256, "TestMemoryManager_ThreadSafe - %d", i );

        threads[i].Create( ThreadProc, (void *)i, name );
    }

    for ( u32 i = 0; i < cThreadNum; ++i )
    {
        threads[i].WaitToEnd();
    }
}